package com.revzone.demo001.utils.DateUtils;

import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class DateConversionUtil {
    private static final List<String> formarts = new ArrayList<>(4);

    private static final String YYYY_MM = "yyyy-MM";
    private static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    static{
        formarts.add(YYYY_MM);
        formarts.add(YYYY_MM_DD);
        formarts.add(YYYY_MM_DD_HH_MM);
        formarts.add(YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 字符串转日期格式
     * @param source
     * @return
     */
    public static Date StringToDate(String source) {
        if (source.equals("") || source.equals(null)) {
            return null;
        }
        source = source.trim();
        if(source.matches("^\\d{4}-\\d{1,2}$")){
            return parseDate(source, formarts.get(0));
        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
            return parseDate(source, formarts.get(1));
        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
            return parseDate(source, formarts.get(2));
        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
            return parseDate(source, formarts.get(3));
        }else {
            throw new IllegalArgumentException("Invalid false value '" + source + "'");
        }

    }

    /**
     * 日期格式转字符串格式
     * @param date
     * @return
     */
    public static String DateToString(Date date,Integer Type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formarts.get(Type));
        String timeString = simpleDateFormat.format(date);
        return timeString;
    }



    /**
     * /时间戳转换日期字符串方法
     * @param value
     * @return
     */
    public static String stampToDateString(String value,Integer Type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formarts.get(Type));
        long lt = new Long(value);
        Date date = new Date(lt);
        String res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * /时间戳转换日期方法
     * @param value
     * @return
     */
    public static Date stampToDate(String value) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(value);
        Date date = new Date(lt);
        String res = simpleDateFormat.format(date);
        Date Udate = StringToDate(res);
        return Udate;
    }


    /**
     * 格式化日期
     * @param dateStr String 字符型日期
     * @param format String 格式
     * @return Date 日期
     */
    private static Date parseDate(String dateStr, String format) {
        Date date;
        try {
            DateFormat dateFormat = new SimpleDateFormat(format);
            date = dateFormat.parse(dateStr);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getLocalizedMessage());
        }
        return date;
    }

    public static void LocalDateTimeToDate(List<Map<String,Object>> list){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (Map<String, Object> userList : list) {
            if (userList.get("create_date") != null){
                String createDateStr = formatter.format((LocalDateTime)userList.get("create_date"));
                userList.put("create_date",createDateStr);
            }  if (userList.get("update_date") != null) {
                String updateDateStr = formatter.format((LocalDateTime)userList.get("update_date"));
                userList.put("update_date",updateDateStr);
            }  if (userList.get("createDate") != null){
                String createDateStr = formatter.format((LocalDateTime)userList.get("createDate"));
                userList.put("createDate",createDateStr);
            }  if (userList.get("updateDate") != null) {
                String updateDateStr = formatter.format((LocalDateTime)userList.get("updateDate"));
                userList.put("updateDate",updateDateStr);
            }
        }
    }


}
